import { Router } from 'express';
import TaskService  from '../service/task';

const taskRouter = Router();

taskRouter.get('/', async (req, res) => {
    const tasks = await new TaskService().findAll();
    res.json(tasks);
});

taskRouter.get('/:id', async (req, res) => {
    const task = await new TaskService().findOne(parseInt(req.params.id));
    res.json(task);
});

taskRouter.post('/', async (req, res) => {
    const task = await new TaskService().create(req.body);
    res.json(task);
});

taskRouter.put('/:id', async (req, res) => {
    const task = await new TaskService().update(parseInt(req.params.id), req.body);
    res.json(task);
});

taskRouter.delete('/:id', async (req, res) => {
    await new TaskService().delete(parseInt(req.params.id));
    res.json({ message: 'Deleted successfully' });
});

export default taskRouter;
